<template>
  <div>
    <el-upload
      action="#"
      list-type="picture-card"
      :on-preview="handlePictureCardPreview"
      :on-remove="handleRemove"
      :file-list="fileList"
      :on-change="handelChange"
      :before-upload="beforeUpload"
      :http-request="upload"
      :limit="1"
      :class="{disabled:fileComputed}"
    >
      <i class="el-icon-plus" />
    </el-upload>
    <el-progress v-if="showPercent" :percentage="percentage" style="width: 200px;" />
    <el-dialog title="图片预览" :visible.sync="dialogVisible">
      <el-row type="flex" justify="center" align="middle">
        <img width="500px" :src="dialogImageUrl" alt="">
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
// 需要实例化
const cos = new COS({
  SecretId: 'AKIDd5IGxNWP1MQEl22rPggPs4zd07xWprcg',
  SecretKey: 'oMdcKmxqSothETSAcniMincARRAcnhKh'
})
// 实例化的包 已经具有了上传的能力 可以上传到该账号里面的存储桶了
export default {
  name: 'Index',
  data() {
    return {
      showPercent: false,
      percentage: 0,
      currentUid: '',
      fileList: [
        {
          url: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg1.baidu.com%2Fit%2Fu%3D3788606852%2C2363382091%26fm%3D253%26app%3D120%26f%3DJPEG%3Fw%3D800%26h%3D800&refer=http%3A%2F%2Fimg1.baidu.com&app=2002&size=w300&q=a80&n=0&g=0n&fmt=jpeg?sec=1645587740&t=ca4f37a877fc0e8adcdddcebb27663cd'
        }
      ],
      dialogImageUrl: '',
      dialogVisible: false
    }
  },
  computed: {
    fileComputed() {
      return this.fileList.length === 1
    }
  },
  methods: {
    handleRemove(file, fileList) {
      // console.log(file, fileList)
      this.fileList = this.fileList.filter(item => !item.uid === file.uid)
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handelChange(file, fileList) {
      this.fileList = fileList
      // console.log(file)
    },
    beforeUpload(file) {
      // console.log(file)
      const types = ['image/jpeg', 'image/gif', 'image/bmp', 'image/png']
      if (!types.includes(file.type)) {
        this.$message.error('上传图片只能是 JPG、GIF、BMP、PNG 格式!')
        return false
      }
      const maxSize = 5 * 1024 * 1024 // 1M=1024KB 1KB = 1024B
      if (maxSize < file.size) {
        this.$message.error('照片大小最大不能超过5M')
        return false
      }
      this.currentUid = file.uid
      this.showPercent = true
      return true
    },
    upload(params) {
      // console.log(params.file)
      // 执行上传操作
      cos.putObject({
        Bucket: 'lss-1309402156', // 存储桶
        Region: 'ap-nanjing', // 地域
        Key: params.file.name, // 文件名
        Body: params.file, // 要上传的文件对象
        StorageClass: 'STANDARD', // 上传的模式类型 直接默认 标准模式即可
        onProgress: (params) => {
          console.log(params.percent * 100)
          this.percentage = params.percent * 100
        }
        // 上传到腾讯云 =》 哪个存储桶 哪个地域的存储桶 文件  格式  名称 回调
      }, (err, data) => {
        // data返回数据之后 应该如何处理
        console.log(err || data.Location)
        if (!err && data.statusCode === 200) {
          console.log('success')
          this.fileList = this.fileList.map(item => {
            if (item.uid === this.currentUid) {
              return {
                ...item,
                url: `http://${data.Location}`,
                upload: true
              }
            }
            return item
          })
        }
        setTimeout(() => {
          this.showPercent = false
          this.percentage = 0
        }, 1000)
      })
    }
  }
}
</script>

<style>
.disabled .el-upload--picture-card {
  display: none;
}
</style>
